Skip to content

Conversation

roberttoyonaga
Copy link
Collaborator

@roberttoyonaga roberttoyonaga commented Oct 3, 2025

This PR fixes a native memory leak in JFR. The problem was reported originally by @joggeli34
here #12253

The problem is that JFR Java event buffers are not being retired/reinstated correctly. Instead of these buffers being reused between recordings, they are leaked. The reason for the leak is because we prematurely reset the javaBuffer thread local when stopping a recording. This is is bad because we need to reuse this thread local to reinstate the buffer in the next recording when a new Java EventWriter is created. Instead, a new buffer gets created and the retired one is lost.

This results in a leak of the size (pageSize > 8 * 1024 ? pageSize : 8 * 1024) * THREAD_COUNT per new recording.

The leak only happens when a recording is stopped and a new recording is started while the same threads are still running. This is the scenario when Java event buffers would be retired/reinstated.
The leak was discovered by @joggeli34 because they are using Pyroscope which starts/stops new recordings every 10s. This frequent new recording creation exacerbates the leak.

I used NMT to investigate this by adding more fine grained JFR NMT categories. Notice that committed memory in category JFR1 increases overtime.
image
image
image
image

After the fix, the memory usage does not increase.

This bug fix should be backported to GraalVM for JDK 25.

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Oct 3, 2025
@roberttoyonaga roberttoyonaga added native-image native-image-jfr and removed OCA Verified All contributors have signed the Oracle Contributor Agreement. labels Oct 3, 2025
@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Oct 3, 2025
@roberttoyonaga roberttoyonaga marked this pull request as ready for review October 3, 2025 16:18
@roberttoyonaga roberttoyonaga changed the title Fix memory leak in Java event TLBs Fix JFR memory leak in Java event TLBs Oct 3, 2025
@christianhaeubl christianhaeubl changed the title Fix JFR memory leak in Java event TLBs [GR-70360] Fix JFR memory leak in Java event TLBs Oct 6, 2025
Copy link
Member

@christianhaeubl christianhaeubl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, changes look good. Will be integrated in the next few days.

@graalvmbot graalvmbot merged commit 99396b5 into oracle:master Oct 7, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
native-image native-image-jfr OCA Verified All contributors have signed the Oracle Contributor Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants